﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using btom = Microsoft.BizTalk.ExplorerOM;

namespace HenIT.BizTalk
{
    public partial class BizTalkGroupSelect : Form
    {
        public BizTalkGroupSelect()
        {
            InitializeComponent();
        }

        public string SqlServer
        {
            get { return cboSqlServer.Text; }
            set { cboSqlServer.Text = value; }
        }
        public string MgmtDb
        {
            get { return txtMgmtDb.Text; }
            set { txtMgmtDb.Text = value; }
        }

        private void cmdTest_Click(object sender, EventArgs e)
        {
            try
            {
                using (btom.BtsCatalogExplorer catalog = new btom.BtsCatalogExplorer())
                {
                    catalog.ConnectionString = "SERVER=" + SqlServer + ";DATABASE=" + MgmtDb + ";Integrated Security=SSPI";
                    MessageBox.Show("Connection valid!\r\nGroup name: " + catalog.GroupName, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Fail", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BizTalkGroupSelect_Load(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.PreviousServers != null)
            {
                foreach (string server in Properties.Settings.Default.PreviousServers)
                {
                    cboSqlServer.Items.Add(server);
                }
            }
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.PreviousServers == null)
            {
                Properties.Settings.Default.PreviousServers = new System.Collections.Specialized.StringCollection();
            }
            if (!Properties.Settings.Default.PreviousServers.Contains(cboSqlServer.Text))
            {
                Properties.Settings.Default.PreviousServers.Add(cboSqlServer.Text);
                Properties.Settings.Default.Save();
            }
        }

    }
}
